package com.ljx.recruitment.interview.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.interview.domain.entity.JobLocation;
import com.ljx.recruitment.interview.domain.vo.req.JobLocationPageReq;
import com.ljx.recruitment.interview.domain.vo.req.JobLocationSubmitReq;
import com.ljx.recruitment.interview.domain.vo.resp.JobLocationResp;
import com.ljx.recruitment.interview.mapper.JobLocationMapper;
import com.ljx.recruitment.interview.service.JobLocationService;
import com.ljx.recruitment.security.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-08  23:11
 */
@Service
public class JobLocationServiceImpl extends ServiceImpl<JobLocationMapper, JobLocation> implements JobLocationService {

    @Autowired
    private JobLocationMapper jobLocationMapper;

    @Override
    public JobLocationResp getInfo(Long jobLocationId) {
        if (Objects.isNull(jobLocationId)){
            throw new ApiException("ID 不能为空");
        }
        JobLocation jobLocation = jobLocationMapper.selectById(jobLocationId);
        return BeanUtil.copyProperties(jobLocation, JobLocationResp.class);
    }

    @Override
    public PageResp<JobLocationResp> getJobLocationPage(JobLocationPageReq req) {
        LambdaQueryWrapper<JobLocation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JobLocation::getTenantId, UserUtil.tryToGetTenantId());
        if (StrUtil.isNotEmpty(req.getKey())) {
            lambdaQueryWrapper.eq(JobLocation::getName, req.getKey());
        }
        if (Objects.nonNull(req.getStatus())) {
            lambdaQueryWrapper.eq(JobLocation::getStatus, req.getStatus());
        }
        Page<JobLocation> page = jobLocationMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);

        return PageResp.<JobLocationResp>builder()
                .total(page.getTotal())
                .list(BeanUtil.copyToList(page.getRecords(), JobLocationResp.class))
                .build();
    }

    @Override
    public JobLocationResp submitJobLocation(JobLocationSubmitReq req) {
        JobLocation jobLocation;
        if (Objects.isNull(req.getId())){
            checkSubmitParams(req);
            jobLocation = BeanUtil.copyProperties(req, JobLocation.class);
            jobLocation.setTenantId(UserUtil.tryToGetTenantId());
            jobLocationMapper.insert(jobLocation);
        } else {
            jobLocation = jobLocationMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, jobLocation);
            jobLocationMapper.updateById(jobLocation);
        }

        return getInfo(jobLocation.getId());
    }

    private void checkSubmitParams(JobLocationSubmitReq req) {
        if (StrUtil.isEmpty(req.getName())){
            throw new ApiException("必要参数不能为空");
        }
    }
}
